package cn.iocoder.boot.springai001.tools;

import cn.hutool.json.JSONUtil;
import cn.iocoder.boot.springai001.service.DBService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ai.tool.annotation.Tool;

import java.sql.SQLException;

/**
 * SQL 工具类
 */
public record ExecuteSqlTools(DBService dbService) {

    private static final Logger logger = LoggerFactory.getLogger(ExecuteSqlTools.class);

//    @Tool(description = "查询AI聊天对话历史记录")
    public String getSpringAiChatMemoryList() {
        String sql = "SELECT content, type FROM SPRING_AI_CHAT_MEMORY";
        try {
            return JSONUtil.toJsonStr(dbService.list(sql));
        } catch (SQLException e) {
            return String.format("{\"error\": \"查询异常\", \"message\": \"%s\"}", e.getMessage());
        }
    }

//    @Tool(description = "执行SQL语句")
    public String executeSql(String sql) {
        try {
            logger.info("执行SQL：{}", sql);
            return JSONUtil.toJsonStr(dbService.list(sql));
        } catch (SQLException e) {
            return String.format("{\"error\": \"查询异常\", \"message\": \"%s\"}", e.getMessage());
        }
    }

}
